<template>
  <div class="login">
    <logo></logo>
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="userName"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit">提交</van-button>
      </div>
    </van-form>
    <router-link :to="{ name: 'Reg' }">没有账号，我要注册</router-link>
  </div>
</template>

<script>
import { Toast } from 'vant';
import Logo from '../components/Logo.vue';
import { loginAPI } from '../services/auth';
import { setToken } from '../utils/tools';

export default {
  data() {
    return {
      username: '',
      password: '',
    };
  },
  components: {
    Logo,
  },
  methods: {
    async onSubmit(values) {
      // console.log('submit', values);
      const res = await loginAPI(values);
      if (res.code === 1) {
        Toast.success('登录成功');
        setToken(res.data);
        this.$router.replace({
          name: 'User',
        });
      } else {
        Toast.fail(res.data);
      }
    },
  },
};
</script>

<style>
.login {
  padding: 20px 0;
}
</style>
